package com.redwine.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.AppCheckVersionResponse;
import com.redwine.app.utils.Utils;
import com.redwine.app.view.AlertDialog;

/**
 * 关于高迪查看版本
 * 作者：gassion on 17/9/9 13:25
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class AboutAppVersionActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.about_iv_app_icon)
    private ImageView mAboutIvAppIcon;
    @ViewInject(R.id.about_tv_version_number)
    private TextView mAboutTvVersionNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_app_version);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        headerTitle.setText(getString(R.string.about_app_name));
        layoutBack.setVisibility(View.VISIBLE);
        mAboutIvAppIcon.setBackgroundResource(R.mipmap.app_icon);
        mAboutTvVersionNumber.setText(BuildConfig.VERSION_NAME);
    }

    @OnClick({R.id.layout_back, R.id.about_ll_cat_new_version})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.about_ll_cat_new_version:
                requestCheckVersion();
                break;

        }
    }

    protected void requestCheckVersion() {
        showWaiting();
        String url = NetworkConstants.APP_UPDATE_URL;
        GsonRequest<AppCheckVersionResponse> request = new GsonRequest<>(url, Request.Method.POST,
                AppCheckVersionResponse.class, false, null, new Response.Listener<AppCheckVersionResponse>() {
            @Override
            public void onResponse(AppCheckVersionResponse response) {
                dismissWaiting();
                if (response.isSuccess()) {
                    AppCheckVersionResponse.DataEntity data = response.getData();
                    if (data.isForcedUpdate()) {
                        //强制更新
                        showAlertDialog(true, data);
                    } else {
                        if (!BuildConfig.VERSION_NAME.equals(data.getVersion())) {
                            showAlertDialog(false, data);
                        } else {
                            Utils.showToast(getString(R.string.about_is_the_latest_version));
                        }
                    }
                } else {
                    Utils.showToast(response.getMsg());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                Utils.showToast(error.getMessage());
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }


    private void showAlertDialog(final boolean isForcedUpdate, final AppCheckVersionResponse.DataEntity data) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AboutAppVersionActivity.this);
        builder.setTitle(getString(R.string.about_check_upgrade));
        builder.setMessage(data.getVersion_info());
        if (!isForcedUpdate) {
            builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
        builder.setPositiveButton(isForcedUpdate ? getString(R.string.about_forced_upgrade) : getString(R.string.about_app_upgrade), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!isForcedUpdate)
                    dialog.dismiss();
                Uri uri = Uri.parse(data.getVersion_url());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        AlertDialog mDialog = builder.create();
        if (isForcedUpdate)
            mDialog.setCancelable(false);
        else
            mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
    }
}
